package com.cyys.modules.system.controller.param;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.annotation.SystemLog;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.JSONData;
import com.cyys.common.validate.Detail;
import com.cyys.common.validate.Edit;
import com.cyys.common.validate.Insert;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.system.model.param.ParamBasicType;
import com.cyys.modules.system.service.param.ParamBasicTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/sys/paramBasicType")
public class ParamBasicTypeAction extends BaseAction {

	@Resource
	private ParamBasicTypeService paramBasicTypeService;

	@GetMapping("/add/input")
	@RequiresPermissions(value = "sys:paramBasic:add")
	public String input(ParamBasicType paramBasicType,Model model)  {
		model.addAttribute("paramBasicType", paramBasicType);
		model.addAttribute("paramBasicMap", ParamBasicUtil.getParamBasic());
		return "module/system/param/paramBasicType-input";
	}

	@ResponseBody
	@PostMapping("/add/save")
	@RequiresPermissions(value = "sys:paramBasic:add")
	@SystemLog(module = "系统参数", methods = "基础参数-保存基础参数")
	public AR save(@Validated(Insert.class)ParamBasicType paramBasicType)  {
		return paramBasicTypeService.save(paramBasicType);
	}

	@GetMapping("/edit/input")
	@RequiresPermissions(value = "sys:paramBasic:edit")
	public String editInput(@Validated(Detail.class)ParamBasicType paramBasicType, Model model)  {
		model.addAttribute("paramBasicType", paramBasicTypeService.getById(paramBasicType.getId()));
		model.addAttribute("paramBasicMap", ParamBasicUtil.getParamBasic());
		return "module/system/param/paramBasicType-input";
	}

	@ResponseBody
	@PostMapping("/edit/save")
	@RequiresPermissions(value = "sys:paramBasic:add")
	@SystemLog(module = "系统参数", methods = "基础参数-更新基础参数")
	public AR edit(@Validated(Edit.class)ParamBasicType paramBasicType)  {
		return paramBasicTypeService.update(paramBasicType);
	}

	@GetMapping("/list")
	@RequiresPermissions("sys:paramBasic:list")
	public String list(String basicId,String basicName, Model model)  {
		model.addAttribute("basicId", basicId);
		model.addAttribute("basicName", basicName);
		return "module/system/param/paramBasicType-list";
	}

	@ResponseBody
	@PostMapping("/del/deleteById")
	@RequiresPermissions("sys:paramBasic:del")
	@SystemLog(module = "系统参数", methods = "基础参数-删除基础参数")
	public AR deleteById(String id) {
		return paramBasicTypeService.deleteById(id);
	}

	@ResponseBody
	@PostMapping("/del/deleteByIds")
	@RequiresPermissions("sys:paramBasic:del")
	@SystemLog(module = "系统参数", methods = "基础参数-批量删除基础参数")
	public AR deleteByIds(String ids)  {
		return paramBasicTypeService.deleteBatchIds(ids);
	}

	@ResponseBody
	@RequestMapping("/list/paramBasicTypeList")
	@RequiresPermissions("sys:paramBasic:list")
	public String paramBasicTypelist(ParamBasicType paramBasicType, Page page)  {
		QueryWrapper qw = new QueryWrapper(paramBasicType);
		qw.orderByAsc("sort");
		return JSONData.toLayerData(paramBasicTypeService.list(page , qw,false));
	}

	@ResponseBody
	@PostMapping("/getByBasicId")
	public AR getByBasicId(String basicId)  {
		return AR.ok(ParamBasicUtil.getParamBasicTypeList().get(basicId),"获取成功");
	}

}
